<!--  -->

<script>
import { mapState, } from 'vuex';
import { PermissionsClass, } from '@/views/project/common/index';
import { stringToArray, } from '@/utils/common';
import _ from 'lodash';
export default {
  components: {
  },
  props: {
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    onlyView: {
      type: Boolean,
      default: false,
    },
    processPanelCode: {
      type: String,
      default: '',
    },
  },
  data() {
    return {

    };
  },
  computed: {
    ...mapState('login', {
      MyID: (v) => {
        return Number(v.userInfo.id) || 0;
      },
      MyPerimissions: (v) => {
        return v.userInfo.permissions || [];
      },
    }),

    State() {
      return this.processPanelCode || this.data.showState;
    },
    Data() {
      return this.data;
    },
    Users() {
      const principal = this.Data.principal;
      // 项目参与人员
      const joinUser = stringToArray(this.Data.joinUser);
      // 任务人员
      const taskUsers = stringToArray(this.Data.taskUsers);
      return {
        principal,
        joinUser,
        taskUsers,
      };
    },
    AllUsers() {
      const value = _.flattenDeep(Object.values(this.Users));
      const users = _.uniq(value.map(Number));
      return users;
    },
    IsInUsers() {
      if (this.AllUsers.includes(this.MyID)) {
        return PermissionsClass.AllUsers;
      }
      return '';
    },
    // 是否是 任务的负责人 或 任务的参与人
    IsTaskPrincipalOrTaskJoin() {
      if (this.Users.taskUsers.includes(this.MyID)) {
        return PermissionsClass.TaskPrincipalOrMember;
      }
      return '';
    },
    IsItemPrincipal() {
      if (this.Users.principal === this.MyID) {
        return PermissionsClass.ProjectPrincipal;
      }
      return '';
    },
    IsITBP() {
      if (this.MyPerimissions.includes('ITBP_MANAGER')) {
        return PermissionsClass.ITBP;
      }
      return '';
    },
    RenderButtonsItem() {
      let options = [];
      if (this.onlyView) {
        options.push({
          name: '查看',
          code: 'searh',
        });
      } else {
        const submit = { name: '提交确认', code: 'confirm', permissionCode: [PermissionsClass.ProjectPrincipal], };
        const confirmResult = { name: '确认结果', code: 'confirmResult', permissionCode: [PermissionsClass.ITBP], };
        const handle = { name: '处理任务', code: 'handle', permissionCode: [PermissionsClass.TaskPrincipalOrMember], };
        const replenish = { name: '任务补充', code: 'handle', permissionCode: [PermissionsClass.TaskPrincipalOrMember], };
        // const change = { name: '变更申请', code: 'change', permissionCode: [PermissionsClass.AllUsers], };

        switch (this.State) {
          case 'implement_start':
            // options = [handle, change];
            options = [handle];

            break;
          // 实施结束待确认
          case 'implement_over':
            // options = [submit, change, replenish];
            options = [submit, replenish];

            break;
          case 'confirmResult':
            // options = [confirmResult, change];
            options = [confirmResult];
            break;
            // 确认结果 通过
          case 'endCompleted':
            options = [];
            break;
            // 确认结果 不通过
          case 'notCompleted':
            options = [];
            break;
          case 'item_before_project':
            // options = [confirmResult, change];
            options = [confirmResult];

            break;
          default:
            options = [];
            break;
        }
      }
      return options;
    },
  },
  mounted() {
  },
  methods: {
  },
  render(h) {
    const buttons = [];
    for (const i of this.RenderButtonsItem) {
      let val = null;
      if (i.permissionCode.includes(this.IsTaskPrincipalOrTaskJoin)) {
        val = i;
      } else if (i.permissionCode.includes(this.IsItemPrincipal)) {
        val = i;
      } else if (i.permissionCode.includes(this.IsInUsers)) {
        val = i;
      } else if (i.permissionCode.includes(this.IsITBP)) {
        val = i;
      } else {
        // /
      }
      if (val) {
        buttons.push(i);
      }
    }
    const look = { name: '查看', code: 'searh', };
    buttons.unshift(look);

    const btns = buttons.map(e => {
      return h('el-button', {
        props: {
          size: 'mini ',
          type: 'text',
        },
        domProps: {
          innerHTML: e.name,
        },
        on: {
          click: () => {
            this.$emit('click', e.code);
          },
        },
      });
    });
    return h('div', {}, btns);
  },
};
</script>
<style lang='scss' scoped>
</style>
